{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用LightGBM/XGboost实现Happy Customer Bank目标客户（贷款成功的客户）识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 首先 import 必要的模块\n",
    "import pandas as pd \n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>Gender</th>\n",
       "      <th>City</th>\n",
       "      <th>Monthly_Income</th>\n",
       "      <th>DOB</th>\n",
       "      <th>DOB_Year</th>\n",
       "      <th>DOB_Month</th>\n",
       "      <th>DOB_Day</th>\n",
       "      <th>Lead_Creation_Date</th>\n",
       "      <th>Lead_Creation_Year</th>\n",
       "      <th>...</th>\n",
       "      <th>Interest_Rate</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Filled_Form</th>\n",
       "      <th>Device_Type</th>\n",
       "      <th>Var2</th>\n",
       "      <th>Source</th>\n",
       "      <th>Var4</th>\n",
       "      <th>LoggedIn</th>\n",
       "      <th>Disbursed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>ID000002C20</td>\n",
       "      <td>Female</td>\n",
       "      <td>Delhi</td>\n",
       "      <td>20000</td>\n",
       "      <td>23-May-78</td>\n",
       "      <td>1978</td>\n",
       "      <td>5</td>\n",
       "      <td>23</td>\n",
       "      <td>15-May-15</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>ID000004E40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Mumbai</td>\n",
       "      <td>35000</td>\n",
       "      <td>7-Oct-85</td>\n",
       "      <td>1985</td>\n",
       "      <td>10</td>\n",
       "      <td>7</td>\n",
       "      <td>4-May-15</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>13.25</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6762.9</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>G</td>\n",
       "      <td>S122</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>ID000007H20</td>\n",
       "      <td>Male</td>\n",
       "      <td>Panchkula</td>\n",
       "      <td>22500</td>\n",
       "      <td>10-Oct-81</td>\n",
       "      <td>1981</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>19-May-15</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>ID000008I30</td>\n",
       "      <td>Male</td>\n",
       "      <td>Saharsa</td>\n",
       "      <td>35000</td>\n",
       "      <td>30-Nov-87</td>\n",
       "      <td>1987</td>\n",
       "      <td>11</td>\n",
       "      <td>30</td>\n",
       "      <td>9-May-15</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S143</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>ID000009J40</td>\n",
       "      <td>Male</td>\n",
       "      <td>Bengaluru</td>\n",
       "      <td>100000</td>\n",
       "      <td>17-Feb-84</td>\n",
       "      <td>1984</td>\n",
       "      <td>2</td>\n",
       "      <td>17</td>\n",
       "      <td>20-May-15</td>\n",
       "      <td>2015</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>N</td>\n",
       "      <td>Web-browser</td>\n",
       "      <td>B</td>\n",
       "      <td>S134</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            ID  Gender       City  Monthly_Income        DOB  DOB_Year  \\\n",
       "0  ID000002C20  Female      Delhi           20000  23-May-78      1978   \n",
       "1  ID000004E40    Male     Mumbai           35000   7-Oct-85      1985   \n",
       "2  ID000007H20    Male  Panchkula           22500  10-Oct-81      1981   \n",
       "3  ID000008I30    Male    Saharsa           35000  30-Nov-87      1987   \n",
       "4  ID000009J40    Male  Bengaluru          100000  17-Feb-84      1984   \n",
       "\n",
       "   DOB_Month  DOB_Day Lead_Creation_Date  Lead_Creation_Year  ...  \\\n",
       "0          5       23          15-May-15                2015  ...   \n",
       "1         10        7           4-May-15                2015  ...   \n",
       "2         10       10          19-May-15                2015  ...   \n",
       "3         11       30           9-May-15                2015  ...   \n",
       "4          2       17          20-May-15                2015  ...   \n",
       "\n",
       "   Interest_Rate  Processing_Fee  EMI_Loan_Submitted  Filled_Form  \\\n",
       "0            NaN             NaN                 NaN            N   \n",
       "1          13.25             NaN              6762.9            N   \n",
       "2            NaN             NaN                 NaN            N   \n",
       "3            NaN             NaN                 NaN            N   \n",
       "4            NaN             NaN                 NaN            N   \n",
       "\n",
       "   Device_Type Var2 Source Var4  LoggedIn Disbursed  \n",
       "0  Web-browser    G   S122    1         0         0  \n",
       "1  Web-browser    G   S122    3         0         0  \n",
       "2  Web-browser    B   S143    1         0         0  \n",
       "3  Web-browser    B   S143    3         0         0  \n",
       "4  Web-browser    B   S134    3         1         0  \n",
       "\n",
       "[5 rows x 32 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取数据\n",
    "# path to where the data lies\n",
    "dpath = './'\n",
    "train = pd.read_csv(dpath +\"Train_utf-8.csv\")\n",
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 87019 entries, 0 to 87018\n",
      "Data columns (total 32 columns):\n",
      "ID                       87019 non-null object\n",
      "Gender                   87019 non-null object\n",
      "City                     86016 non-null object\n",
      "Monthly_Income           87019 non-null int64\n",
      "DOB                      87019 non-null object\n",
      "DOB_Year                 87019 non-null int64\n",
      "DOB_Month                87019 non-null int64\n",
      "DOB_Day                  87019 non-null int64\n",
      "Lead_Creation_Date       87019 non-null object\n",
      "Lead_Creation_Year       87019 non-null int64\n",
      "Lead_Creation_Month      87019 non-null int64\n",
      "Lead_Creation_Day        87019 non-null int64\n",
      "Loan_Amount_Applied      86948 non-null float64\n",
      "Loan_Tenure_Applied      86948 non-null float64\n",
      "Existing_EMI             86948 non-null float64\n",
      "Employer_Name            86948 non-null object\n",
      "Salary_Account           75255 non-null object\n",
      "Mobile_Verified          87019 non-null object\n",
      "Var5                     87019 non-null int64\n",
      "Var1                     87019 non-null object\n",
      "Loan_Amount_Submitted    52407 non-null float64\n",
      "Loan_Tenure_Submitted    52407 non-null float64\n",
      "Interest_Rate            27726 non-null float64\n",
      "Processing_Fee           27420 non-null float64\n",
      "EMI_Loan_Submitted       27726 non-null float64\n",
      "Filled_Form              87019 non-null object\n",
      "Device_Type              87019 non-null object\n",
      "Var2                     87019 non-null object\n",
      "Source                   87019 non-null object\n",
      "Var4                     87019 non-null int64\n",
      "LoggedIn                 87019 non-null int64\n",
      "Disbursed                87019 non-null int64\n",
      "dtypes: float64(8), int64(11), object(13)\n",
      "memory usage: 21.2+ MB\n"
     ]
    }
   ],
   "source": [
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Monthly_Income</th>\n",
       "      <th>DOB_Year</th>\n",
       "      <th>DOB_Month</th>\n",
       "      <th>DOB_Day</th>\n",
       "      <th>Lead_Creation_Year</th>\n",
       "      <th>Lead_Creation_Month</th>\n",
       "      <th>Lead_Creation_Day</th>\n",
       "      <th>Loan_Amount_Applied</th>\n",
       "      <th>Loan_Tenure_Applied</th>\n",
       "      <th>Existing_EMI</th>\n",
       "      <th>Var5</th>\n",
       "      <th>Loan_Amount_Submitted</th>\n",
       "      <th>Loan_Tenure_Submitted</th>\n",
       "      <th>Interest_Rate</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Var4</th>\n",
       "      <th>LoggedIn</th>\n",
       "      <th>Disbursed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>8.701900e+04</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.0</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>8.694800e+04</td>\n",
       "      <td>86948.000000</td>\n",
       "      <td>8.694800e+04</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>5.240700e+04</td>\n",
       "      <td>52407.000000</td>\n",
       "      <td>27726.000000</td>\n",
       "      <td>27420.000000</td>\n",
       "      <td>27726.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "      <td>87019.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>5.885053e+04</td>\n",
       "      <td>1984.116446</td>\n",
       "      <td>6.315115</td>\n",
       "      <td>14.253623</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>6.069341</td>\n",
       "      <td>16.088486</td>\n",
       "      <td>2.302533e+05</td>\n",
       "      <td>2.131423</td>\n",
       "      <td>3.696270e+03</td>\n",
       "      <td>4.961560</td>\n",
       "      <td>3.950106e+05</td>\n",
       "      <td>3.891369</td>\n",
       "      <td>19.197474</td>\n",
       "      <td>5131.150839</td>\n",
       "      <td>10999.528377</td>\n",
       "      <td>2.949827</td>\n",
       "      <td>0.029350</td>\n",
       "      <td>0.014629</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.177524e+06</td>\n",
       "      <td>7.093963</td>\n",
       "      <td>3.311007</td>\n",
       "      <td>8.902527</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.827163</td>\n",
       "      <td>8.950613</td>\n",
       "      <td>3.542079e+05</td>\n",
       "      <td>2.014192</td>\n",
       "      <td>3.981044e+04</td>\n",
       "      <td>5.670393</td>\n",
       "      <td>3.082481e+05</td>\n",
       "      <td>1.165359</td>\n",
       "      <td>5.834213</td>\n",
       "      <td>4725.837644</td>\n",
       "      <td>7512.323050</td>\n",
       "      <td>1.697717</td>\n",
       "      <td>0.168786</td>\n",
       "      <td>0.120063</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>1932.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>5.000000e+04</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>11.990000</td>\n",
       "      <td>200.000000</td>\n",
       "      <td>1176.410000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.650000e+04</td>\n",
       "      <td>1981.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>8.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>2.000000e+05</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>15.250000</td>\n",
       "      <td>2000.000000</td>\n",
       "      <td>6491.600000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.500000e+04</td>\n",
       "      <td>1986.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>14.000000</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>16.000000</td>\n",
       "      <td>1.000000e+05</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>3.000000e+05</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>4000.000000</td>\n",
       "      <td>9392.970000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>4.000000e+04</td>\n",
       "      <td>1989.000000</td>\n",
       "      <td>9.000000</td>\n",
       "      <td>22.000000</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>24.000000</td>\n",
       "      <td>3.000000e+05</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>3.500000e+03</td>\n",
       "      <td>11.000000</td>\n",
       "      <td>5.000000e+05</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>6250.000000</td>\n",
       "      <td>12919.040000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>4.445544e+08</td>\n",
       "      <td>2029.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>2015.0</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>1.000000e+07</td>\n",
       "      <td>18.000000</td>\n",
       "      <td>3.000000e+06</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>37.000000</td>\n",
       "      <td>50000.000000</td>\n",
       "      <td>144748.280000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Monthly_Income      DOB_Year     DOB_Month       DOB_Day  \\\n",
       "count    8.701900e+04  87019.000000  87019.000000  87019.000000   \n",
       "mean     5.885053e+04   1984.116446      6.315115     14.253623   \n",
       "std      2.177524e+06      7.093963      3.311007      8.902527   \n",
       "min      0.000000e+00   1932.000000      1.000000      1.000000   \n",
       "25%      1.650000e+04   1981.000000      4.000000      6.000000   \n",
       "50%      2.500000e+04   1986.000000      6.000000     14.000000   \n",
       "75%      4.000000e+04   1989.000000      9.000000     22.000000   \n",
       "max      4.445544e+08   2029.000000     12.000000     31.000000   \n",
       "\n",
       "       Lead_Creation_Year  Lead_Creation_Month  Lead_Creation_Day  \\\n",
       "count             87019.0         87019.000000       87019.000000   \n",
       "mean               2015.0             6.069341          16.088486   \n",
       "std                   0.0             0.827163           8.950613   \n",
       "min                2015.0             5.000000           1.000000   \n",
       "25%                2015.0             5.000000           8.000000   \n",
       "50%                2015.0             6.000000          16.000000   \n",
       "75%                2015.0             7.000000          24.000000   \n",
       "max                2015.0             7.000000          31.000000   \n",
       "\n",
       "       Loan_Amount_Applied  Loan_Tenure_Applied  Existing_EMI          Var5  \\\n",
       "count         8.694800e+04         86948.000000  8.694800e+04  87019.000000   \n",
       "mean          2.302533e+05             2.131423  3.696270e+03      4.961560   \n",
       "std           3.542079e+05             2.014192  3.981044e+04      5.670393   \n",
       "min           0.000000e+00             0.000000  0.000000e+00      0.000000   \n",
       "25%           0.000000e+00             0.000000  0.000000e+00      0.000000   \n",
       "50%           1.000000e+05             2.000000  0.000000e+00      2.000000   \n",
       "75%           3.000000e+05             4.000000  3.500000e+03     11.000000   \n",
       "max           1.000000e+07            10.000000  1.000000e+07     18.000000   \n",
       "\n",
       "       Loan_Amount_Submitted  Loan_Tenure_Submitted  Interest_Rate  \\\n",
       "count           5.240700e+04           52407.000000   27726.000000   \n",
       "mean            3.950106e+05               3.891369      19.197474   \n",
       "std             3.082481e+05               1.165359       5.834213   \n",
       "min             5.000000e+04               1.000000      11.990000   \n",
       "25%             2.000000e+05               3.000000      15.250000   \n",
       "50%             3.000000e+05               4.000000      18.000000   \n",
       "75%             5.000000e+05               5.000000      20.000000   \n",
       "max             3.000000e+06               6.000000      37.000000   \n",
       "\n",
       "       Processing_Fee  EMI_Loan_Submitted          Var4      LoggedIn  \\\n",
       "count    27420.000000        27726.000000  87019.000000  87019.000000   \n",
       "mean      5131.150839        10999.528377      2.949827      0.029350   \n",
       "std       4725.837644         7512.323050      1.697717      0.168786   \n",
       "min        200.000000         1176.410000      0.000000      0.000000   \n",
       "25%       2000.000000         6491.600000      1.000000      0.000000   \n",
       "50%       4000.000000         9392.970000      3.000000      0.000000   \n",
       "75%       6250.000000        12919.040000      5.000000      0.000000   \n",
       "max      50000.000000       144748.280000      7.000000      1.000000   \n",
       "\n",
       "          Disbursed  \n",
       "count  87019.000000  \n",
       "mean       0.014629  \n",
       "std        0.120063  \n",
       "min        0.000000  \n",
       "25%        0.000000  \n",
       "50%        0.000000  \n",
       "75%        0.000000  \n",
       "max        1.000000  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 各属性的统计特性\n",
    "train.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从结果中我们可以看到很多列的最小值为0。而在一些特定列代表的变量中，0值并没有意义，这就表名该值无效或为缺失值。\n",
    "\n",
    "在Pandas的DataFrame中，通过replace()函数可以很方便的将我们感兴趣的数据子集的值标记为NaN。\n",
    "\n",
    "标记完缺失值之后，可以利用isnull()函数将数据集中所有的NaN值标记为True，然后就可以得到每一列中缺失值的数量了。\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ID                           0\n",
      "Gender                       0\n",
      "City                      1003\n",
      "Monthly_Income               0\n",
      "DOB                          0\n",
      "DOB_Year                     0\n",
      "DOB_Month                    0\n",
      "DOB_Day                      0\n",
      "Lead_Creation_Date           0\n",
      "Lead_Creation_Year           0\n",
      "Lead_Creation_Month          0\n",
      "Lead_Creation_Day            0\n",
      "Loan_Amount_Applied      28923\n",
      "Loan_Tenure_Applied      33914\n",
      "Existing_EMI             58308\n",
      "Employer_Name               71\n",
      "Salary_Account           11764\n",
      "Mobile_Verified              0\n",
      "Var5                         0\n",
      "Var1                         0\n",
      "Loan_Amount_Submitted    34612\n",
      "Loan_Tenure_Submitted    34612\n",
      "Interest_Rate            59293\n",
      "Processing_Fee           59599\n",
      "EMI_Loan_Submitted       59293\n",
      "Filled_Form                  0\n",
      "Device_Type                  0\n",
      "Var2                         0\n",
      "Source                       0\n",
      "Var4                         0\n",
      "LoggedIn                     0\n",
      "Disbursed                    0\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "NaN_col_names = ['City','Loan_Amount_Applied','Loan_Tenure_Applied','Existing_EMI','Salary_Account','Loan_Amount_Submitted','Loan_Tenure_Submitted','Interest_Rate','Processing_Fee','EMI_Loan_Submitted']\n",
    "train[NaN_col_names] = train[NaN_col_names].replace(0, np.NaN)\n",
    "print(train.isnull().sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ID                       0\n",
       "Gender                   0\n",
       "City                     0\n",
       "Monthly_Income           0\n",
       "DOB                      0\n",
       "DOB_Year                 0\n",
       "DOB_Month                0\n",
       "DOB_Day                  0\n",
       "Lead_Creation_Date       0\n",
       "Lead_Creation_Year       0\n",
       "Lead_Creation_Month      0\n",
       "Lead_Creation_Day        0\n",
       "Loan_Amount_Applied      1\n",
       "Loan_Tenure_Applied      1\n",
       "Existing_EMI             1\n",
       "Employer_Name            0\n",
       "Salary_Account           1\n",
       "Mobile_Verified          0\n",
       "Var5                     0\n",
       "Var1                     0\n",
       "Loan_Amount_Submitted    0\n",
       "Loan_Tenure_Submitted    0\n",
       "Interest_Rate            0\n",
       "Processing_Fee           0\n",
       "EMI_Loan_Submitted       0\n",
       "Filled_Form              0\n",
       "Device_Type              0\n",
       "Var2                     0\n",
       "Source                   0\n",
       "Var4                     0\n",
       "LoggedIn                 0\n",
       "Disbursed                0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train=train.fillna(method = 'backfill', axis = 0)\n",
    "#train_data.interpolate(method = 'linear', axis = 0)\n",
    "#test_data=test_data.fillna(method = 'backfill', axis = 0)\n",
    "train.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "类别型特征编码\n",
    "\n",
    "对类别型特征进行独热编码\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Gender_Female</th>\n",
       "      <th>Gender_Male</th>\n",
       "      <th>Mobile_Verified_N</th>\n",
       "      <th>Mobile_Verified_Y</th>\n",
       "      <th>Var1_HAVC</th>\n",
       "      <th>Var1_HAXA</th>\n",
       "      <th>Var1_HAXB</th>\n",
       "      <th>Var1_HAXC</th>\n",
       "      <th>Var1_HAXF</th>\n",
       "      <th>Var1_HAXM</th>\n",
       "      <th>...</th>\n",
       "      <th>Source_S153</th>\n",
       "      <th>Source_S154</th>\n",
       "      <th>Source_S155</th>\n",
       "      <th>Source_S156</th>\n",
       "      <th>Source_S157</th>\n",
       "      <th>Source_S158</th>\n",
       "      <th>Source_S159</th>\n",
       "      <th>Source_S160</th>\n",
       "      <th>Source_S161</th>\n",
       "      <th>Source_S162</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 64 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Gender_Female  Gender_Male  Mobile_Verified_N  Mobile_Verified_Y  \\\n",
       "0              1            0                  1                  0   \n",
       "1              0            1                  0                  1   \n",
       "2              0            1                  0                  1   \n",
       "3              0            1                  0                  1   \n",
       "4              0            1                  0                  1   \n",
       "\n",
       "   Var1_HAVC  Var1_HAXA  Var1_HAXB  Var1_HAXC  Var1_HAXF  Var1_HAXM  ...  \\\n",
       "0          0          0          0          0          0          0  ...   \n",
       "1          0          0          0          0          0          0  ...   \n",
       "2          0          0          0          0          0          0  ...   \n",
       "3          0          0          0          0          0          0  ...   \n",
       "4          0          0          0          0          0          0  ...   \n",
       "\n",
       "   Source_S153  Source_S154  Source_S155  Source_S156  Source_S157  \\\n",
       "0            0            0            0            0            0   \n",
       "1            0            0            0            0            0   \n",
       "2            0            0            0            0            0   \n",
       "3            0            0            0            0            0   \n",
       "4            0            0            0            0            0   \n",
       "\n",
       "   Source_S158  Source_S159  Source_S160  Source_S161  Source_S162  \n",
       "0            0            0            0            0            0  \n",
       "1            0            0            0            0            0  \n",
       "2            0            0            0            0            0  \n",
       "3            0            0            0            0            0  \n",
       "4            0            0            0            0            0  \n",
       "\n",
       "[5 rows x 64 columns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对类别型特征，观察其取值范围及直方图\n",
    "categorical_features = ['Gender','Mobile_Verified','Var1','Filled_Form','Device_Type','Var2','Source']\n",
    "\n",
    "#数据类型变为object，才能被get_dummies处理\n",
    "for col in categorical_features:\n",
    "    train[col] = train[col].astype('object')\n",
    "    \n",
    "X_train_cat = train[categorical_features]\n",
    "X_train_cat = pd.get_dummies(X_train_cat)\n",
    "X_train_cat.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 87019 entries, 0 to 87018\n",
      "Data columns (total 64 columns):\n",
      "Gender_Female              87019 non-null uint8\n",
      "Gender_Male                87019 non-null uint8\n",
      "Mobile_Verified_N          87019 non-null uint8\n",
      "Mobile_Verified_Y          87019 non-null uint8\n",
      "Var1_HAVC                  87019 non-null uint8\n",
      "Var1_HAXA                  87019 non-null uint8\n",
      "Var1_HAXB                  87019 non-null uint8\n",
      "Var1_HAXC                  87019 non-null uint8\n",
      "Var1_HAXF                  87019 non-null uint8\n",
      "Var1_HAXM                  87019 non-null uint8\n",
      "Var1_HAYT                  87019 non-null uint8\n",
      "Var1_HAZD                  87019 non-null uint8\n",
      "Var1_HBXA                  87019 non-null uint8\n",
      "Var1_HBXB                  87019 non-null uint8\n",
      "Var1_HBXC                  87019 non-null uint8\n",
      "Var1_HBXD                  87019 non-null uint8\n",
      "Var1_HBXH                  87019 non-null uint8\n",
      "Var1_HBXX                  87019 non-null uint8\n",
      "Var1_HCXD                  87019 non-null uint8\n",
      "Var1_HCXF                  87019 non-null uint8\n",
      "Var1_HCXG                  87019 non-null uint8\n",
      "Var1_HCYS                  87019 non-null uint8\n",
      "Var1_HVYS                  87019 non-null uint8\n",
      "Filled_Form_N              87019 non-null uint8\n",
      "Filled_Form_Y              87019 non-null uint8\n",
      "Device_Type_Mobile         87019 non-null uint8\n",
      "Device_Type_Web-browser    87019 non-null uint8\n",
      "Var2_A                     87019 non-null uint8\n",
      "Var2_B                     87019 non-null uint8\n",
      "Var2_C                     87019 non-null uint8\n",
      "Var2_D                     87019 non-null uint8\n",
      "Var2_E                     87019 non-null uint8\n",
      "Var2_F                     87019 non-null uint8\n",
      "Var2_G                     87019 non-null uint8\n",
      "Source_S122                87019 non-null uint8\n",
      "Source_S123                87019 non-null uint8\n",
      "Source_S124                87019 non-null uint8\n",
      "Source_S125                87019 non-null uint8\n",
      "Source_S127                87019 non-null uint8\n",
      "Source_S129                87019 non-null uint8\n",
      "Source_S130                87019 non-null uint8\n",
      "Source_S133                87019 non-null uint8\n",
      "Source_S134                87019 non-null uint8\n",
      "Source_S135                87019 non-null uint8\n",
      "Source_S136                87019 non-null uint8\n",
      "Source_S137                87019 non-null uint8\n",
      "Source_S138                87019 non-null uint8\n",
      "Source_S139                87019 non-null uint8\n",
      "Source_S140                87019 non-null uint8\n",
      "Source_S141                87019 non-null uint8\n",
      "Source_S143                87019 non-null uint8\n",
      "Source_S144                87019 non-null uint8\n",
      "Source_S150                87019 non-null uint8\n",
      "Source_S151                87019 non-null uint8\n",
      "Source_S153                87019 non-null uint8\n",
      "Source_S154                87019 non-null uint8\n",
      "Source_S155                87019 non-null uint8\n",
      "Source_S156                87019 non-null uint8\n",
      "Source_S157                87019 non-null uint8\n",
      "Source_S158                87019 non-null uint8\n",
      "Source_S159                87019 non-null uint8\n",
      "Source_S160                87019 non-null uint8\n",
      "Source_S161                87019 non-null uint8\n",
      "Source_S162                87019 non-null uint8\n",
      "dtypes: uint8(64)\n",
      "memory usage: 5.3 MB\n"
     ]
    }
   ],
   "source": [
    "X_train_cat.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Gender_Female</th>\n",
       "      <th>Gender_Male</th>\n",
       "      <th>Mobile_Verified_N</th>\n",
       "      <th>Mobile_Verified_Y</th>\n",
       "      <th>Var1_HAVC</th>\n",
       "      <th>Var1_HAXA</th>\n",
       "      <th>Var1_HAXB</th>\n",
       "      <th>Var1_HAXC</th>\n",
       "      <th>Var1_HAXF</th>\n",
       "      <th>Var1_HAXM</th>\n",
       "      <th>...</th>\n",
       "      <th>Loan_Tenure_Applied</th>\n",
       "      <th>Existing_EMI</th>\n",
       "      <th>Var5</th>\n",
       "      <th>Loan_Amount_Submitted</th>\n",
       "      <th>Loan_Tenure_Submitted</th>\n",
       "      <th>Interest_Rate</th>\n",
       "      <th>Processing_Fee</th>\n",
       "      <th>EMI_Loan_Submitted</th>\n",
       "      <th>Var4</th>\n",
       "      <th>Disbursed</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>5.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>0</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>13.25</td>\n",
       "      <td>1500.0</td>\n",
       "      <td>6762.90</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>13</td>\n",
       "      <td>200000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>13.25</td>\n",
       "      <td>1500.0</td>\n",
       "      <td>6762.90</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>4.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>0</td>\n",
       "      <td>450000.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>13.99</td>\n",
       "      <td>1500.0</td>\n",
       "      <td>6978.92</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>5.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>10</td>\n",
       "      <td>920000.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>13.99</td>\n",
       "      <td>1500.0</td>\n",
       "      <td>6978.92</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>2.0</td>\n",
       "      <td>25000.0</td>\n",
       "      <td>17</td>\n",
       "      <td>500000.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>13.99</td>\n",
       "      <td>1500.0</td>\n",
       "      <td>6978.92</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 82 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Gender_Female  Gender_Male  Mobile_Verified_N  Mobile_Verified_Y  \\\n",
       "0              1            0                  1                  0   \n",
       "1              0            1                  0                  1   \n",
       "2              0            1                  0                  1   \n",
       "3              0            1                  0                  1   \n",
       "4              0            1                  0                  1   \n",
       "\n",
       "   Var1_HAVC  Var1_HAXA  Var1_HAXB  Var1_HAXC  Var1_HAXF  Var1_HAXM  ...  \\\n",
       "0          0          0          0          0          0          0  ...   \n",
       "1          0          0          0          0          0          0  ...   \n",
       "2          0          0          0          0          0          0  ...   \n",
       "3          0          0          0          0          0          0  ...   \n",
       "4          0          0          0          0          0          0  ...   \n",
       "\n",
       "   Loan_Tenure_Applied  Existing_EMI  Var5  Loan_Amount_Submitted  \\\n",
       "0                  5.0       25000.0     0               200000.0   \n",
       "1                  2.0       25000.0    13               200000.0   \n",
       "2                  4.0       25000.0     0               450000.0   \n",
       "3                  5.0       25000.0    10               920000.0   \n",
       "4                  2.0       25000.0    17               500000.0   \n",
       "\n",
       "   Loan_Tenure_Submitted  Interest_Rate  Processing_Fee  EMI_Loan_Submitted  \\\n",
       "0                    2.0          13.25          1500.0             6762.90   \n",
       "1                    2.0          13.25          1500.0             6762.90   \n",
       "2                    4.0          13.99          1500.0             6978.92   \n",
       "3                    5.0          13.99          1500.0             6978.92   \n",
       "4                    2.0          13.99          1500.0             6978.92   \n",
       "\n",
       "   Var4  Disbursed  \n",
       "0     1          0  \n",
       "1     3          0  \n",
       "2     1          0  \n",
       "3     3          0  \n",
       "4     3          0  \n",
       "\n",
       "[5 rows x 82 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标签\n",
    "y_train = train['Disbursed']   #形式为Class_x\n",
    "\n",
    "#暂存id，其实id没什么用\n",
    "train_id = train['ID']\n",
    "# drop ids and get labels\n",
    "X_train = train.drop([\"ID\", \"LoggedIn\",\"DOB\",\"Lead_Creation_Date\",\"Employer_Name\",'Gender','City','Salary_Account','Mobile_Verified','Var1','Filled_Form','Device_Type','Var2','Source'], axis=1)\n",
    "X_train = pd.concat([X_train_cat, X_train], axis = 1, ignore_index=False)\n",
    "X_train.head()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 87019 entries, 0 to 87018\n",
      "Data columns (total 83 columns):\n",
      "Gender_Female              87019 non-null float64\n",
      "Gender_Male                87019 non-null float64\n",
      "Mobile_Verified_N          87019 non-null float64\n",
      "Mobile_Verified_Y          87019 non-null float64\n",
      "Var1_HAVC                  87019 non-null float64\n",
      "Var1_HAXA                  87019 non-null float64\n",
      "Var1_HAXB                  87019 non-null float64\n",
      "Var1_HAXC                  87019 non-null float64\n",
      "Var1_HAXF                  87019 non-null float64\n",
      "Var1_HAXM                  87019 non-null float64\n",
      "Var1_HAYT                  87019 non-null float64\n",
      "Var1_HAZD                  87019 non-null float64\n",
      "Var1_HBXA                  87019 non-null float64\n",
      "Var1_HBXB                  87019 non-null float64\n",
      "Var1_HBXC                  87019 non-null float64\n",
      "Var1_HBXD                  87019 non-null float64\n",
      "Var1_HBXH                  87019 non-null float64\n",
      "Var1_HBXX                  87019 non-null float64\n",
      "Var1_HCXD                  87019 non-null float64\n",
      "Var1_HCXF                  87019 non-null float64\n",
      "Var1_HCXG                  87019 non-null float64\n",
      "Var1_HCYS                  87019 non-null float64\n",
      "Var1_HVYS                  87019 non-null float64\n",
      "Filled_Form_N              87019 non-null float64\n",
      "Filled_Form_Y              87019 non-null float64\n",
      "Device_Type_Mobile         87019 non-null float64\n",
      "Device_Type_Web-browser    87019 non-null float64\n",
      "Var2_A                     87019 non-null float64\n",
      "Var2_B                     87019 non-null float64\n",
      "Var2_C                     87019 non-null float64\n",
      "Var2_D                     87019 non-null float64\n",
      "Var2_E                     87019 non-null float64\n",
      "Var2_F                     87019 non-null float64\n",
      "Var2_G                     87019 non-null float64\n",
      "Source_S122                87019 non-null float64\n",
      "Source_S123                87019 non-null float64\n",
      "Source_S124                87019 non-null float64\n",
      "Source_S125                87019 non-null float64\n",
      "Source_S127                87019 non-null float64\n",
      "Source_S129                87019 non-null float64\n",
      "Source_S130                87019 non-null float64\n",
      "Source_S133                87019 non-null float64\n",
      "Source_S134                87019 non-null float64\n",
      "Source_S135                87019 non-null float64\n",
      "Source_S136                87019 non-null float64\n",
      "Source_S137                87019 non-null float64\n",
      "Source_S138                87019 non-null float64\n",
      "Source_S139                87019 non-null float64\n",
      "Source_S140                87019 non-null float64\n",
      "Source_S141                87019 non-null float64\n",
      "Source_S143                87019 non-null float64\n",
      "Source_S144                87019 non-null float64\n",
      "Source_S150                87019 non-null float64\n",
      "Source_S151                87019 non-null float64\n",
      "Source_S153                87019 non-null float64\n",
      "Source_S154                87019 non-null float64\n",
      "Source_S155                87019 non-null float64\n",
      "Source_S156                87019 non-null float64\n",
      "Source_S157                87019 non-null float64\n",
      "Source_S158                87019 non-null float64\n",
      "Source_S159                87019 non-null float64\n",
      "Source_S160                87019 non-null float64\n",
      "Source_S161                87019 non-null float64\n",
      "Source_S162                87019 non-null float64\n",
      "Monthly_Income             87019 non-null float64\n",
      "DOB_Year                   87019 non-null float64\n",
      "DOB_Month                  87019 non-null float64\n",
      "DOB_Day                    87019 non-null float64\n",
      "Lead_Creation_Year         87019 non-null float64\n",
      "Lead_Creation_Month        87019 non-null float64\n",
      "Lead_Creation_Day          87019 non-null float64\n",
      "Loan_Amount_Applied        87018 non-null float64\n",
      "Loan_Tenure_Applied        87018 non-null float64\n",
      "Existing_EMI               87018 non-null float64\n",
      "Var5                       87019 non-null float64\n",
      "Loan_Amount_Submitted      87019 non-null float64\n",
      "Loan_Tenure_Submitted      87019 non-null float64\n",
      "Interest_Rate              87019 non-null float64\n",
      "Processing_Fee             87019 non-null float64\n",
      "EMI_Loan_Submitted         87019 non-null float64\n",
      "Var4                       87019 non-null float64\n",
      "Disbursed                  87019 non-null float64\n",
      "Disbursed                  87019 non-null int64\n",
      "dtypes: float64(82), int64(1)\n",
      "memory usage: 55.1 MB\n"
     ]
    }
   ],
   "source": [
    "train.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda3\\lib\\site-packages\\sklearn\\preprocessing\\data.py:645: DataConversionWarning: Data with input dtype uint8, int64, float64 were all converted to float64 by StandardScaler.\n",
      "  return self.partial_fit(X, y)\n",
      "D:\\Anaconda3\\lib\\site-packages\\sklearn\\base.py:464: DataConversionWarning: Data with input dtype uint8, int64, float64 were all converted to float64 by StandardScaler.\n",
      "  return self.fit(X, **fit_params).transform(X)\n"
     ]
    }
   ],
   "source": [
    "#用于保存特征工程之后的结果\n",
    "feat_names = X_train.columns\n",
    "\n",
    "# 数据标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# 初始化特征的标准化器\n",
    "ss_X = StandardScaler()\n",
    "\n",
    "# 分别对训练和测试数据的特征进行标准化处理\n",
    "X_train = ss_X.fit_transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#存为csv格式\n",
    "X_train = pd.DataFrame(columns = feat_names, data = X_train)\n",
    "\n",
    "train = pd.concat([X_train, y_train], axis = 1)\n",
    "\n",
    "train.to_csv('FE_Train2.csv',index = False,header=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
